package dao;

import pojo.Tcm;
import pojo.TcmRelation;
import util.DbUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TcmRelationDao {
    private Connection conn = null;
    private PreparedStatement pstm = null;
    private ResultSet rs = null;
//    获取tcmid对应的心得和出处
    public List<TcmRelation> getMessByTcmId(int tcmId){
        List<TcmRelation> list = new ArrayList<>();
        try{
            conn  = DbUtil.getConnection();
            String sql = "select * from tcmrelation where tcmid="+tcmId;
            pstm = conn.prepareStatement(sql);
            rs=pstm.executeQuery(sql);
            while(rs.next()){
                TcmRelation tcmRelation=new TcmRelation();
                tcmRelation.setId(Integer.parseInt(rs.getString("id")));
                tcmRelation.setProvenance(rs.getString("provenance"));
                tcmRelation.setContent(rs.getString("content"));
                tcmRelation.setTcmid(Integer.parseInt(rs.getString("tcmid")));
                list.add(tcmRelation);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally{
            DbUtil.close(conn, pstm, rs);
        }
        return list;
    }
}
